// pages/options/options.js
var util = require('../../utils/util.js');
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navindex: 0,
    scrollTop: 0,
    scrollHeight:'',
    hidden:true,
    chose_c:true,
    
    martop:'170',
    tabbarIndex:'',
    freightnav: [
      { txt: '全部' },
      { txt: '熟车' },
    ],
    showTop:true,
    choseB: [],
    choseC: [],
    // freight: [
    //   {
    //     time: '40分钟', pic: '/images/icon.png', name: '刘师傅', legend: '50KM', numbers: '冀A1234', long: '6.8米/高栏', city: '西安', distance:'20'
    //   } 
    // ],


    freight:[],
    currentPage: 1,
    deliver_city: '',
    accept_city: '',
    car_length: '',
    totalPage: 1,
    car_type: '',
    buyindex:0,
    xingindex:0,
    ismy:1,
    isXiala:0,
    uData:[],
    isShowTips:false,
    currentpage1:1,
    currentpage2:1,
    showShu: true,

  },
  //司机详情
  optionsdeil(e){
    var id = e.currentTarget.dataset.id;
    app.navTo('../optionsdeil/optionsdeil?id=' +id)
  },
  changecity: function (cityName, type) {
    if (type == 0) {
      // console.log("出发城市" + cityName)
      this.setData({
        city: cityName,
        currentPage: 1,
        deliver_city: cityName,
        freight: []

      })
      // var base = getApp().data.base_url;
      this.initData()
    }
    else if (type == 1) {
      // console.log("送货城市" + cityName)
      this.setData({
        city1: cityName,
        currentPage: 1,
        accept_city: cityName,
        freight: []
      })

      this.initData()
    }
  },
  confirm_car_type() {
    // 车长车型的选择确定按钮
    // 暂无功能，仅为收起选择面板
    this.typebuy();
  },
  initCartLength: function () {
    var that = this;
    util.commonRequest('carLength', 'post', {}, function (res) {
      if (res.data.code == 200) {
        that.setData({
          choseB: res.data.data
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },
  initCartType: function () {
    var that = this;
    util.commonRequest('carType', 'post', {}, function (res) {
      if (res.data.code == 200) {
        that.setData({
          choseC: res.data.data
        })
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },
  initData: function () {
    var that = this;

    var deliver_city = that.data.deliver_city
    var accept_city = that.data.accept_city
    if (deliver_city == '不限') { deliver_city = ''; }
    if (accept_city == '不限') { accept_city = ''; }


    var param = {
      currentPage: that.data.currentPage,
      deliver_city: deliver_city,
      accept_city: accept_city,
      car_length: that.data.car_length,
      car_type: that.data.car_type,
      lat: wx.getStorageSync('lat'),
      lon: wx.getStorageSync('lon'),
      judceApi: that.data.ismy
    };
    //judceApi 1 我的车辆 
    var rquestUrl = 'deliverList';
    wx.showLoading();
    // var judceApi = that.data.ismy;
    // if (judceApi == 1) //我的车辆
    // {
    //   rquestUrl = "myCarList";
    // }else{
    //                     //车源列表
    //   rquestUrl = "deliverList";
    // }

    util.commonRequest('myCarList', 'post', param, function (res) {
      var data = res.data;
      wx.hideLoading();
      if (data.code == 200) {
        var tempArr = data.data.data;
        var oriArr = that.data.freight;
        if (that.data.isXiala == 1){
          var finalyArr = tempArr
        } else{
          var finalyArr = oriArr.concat(tempArr);
        }
        if (finalyArr.length == 0){
          that.setData({
            isShowTips:true
          })
        }
        that.setData({
          freight: finalyArr,
          totalPage: res.data.data.last_page
        })

      //  console.log(res.data.data)

      } else {
        wx.showToast({
          icon: 'loading',
          title: data.msg,
        })
      }
    });
  },
  //加入熟车
  chosecar:function(e){

    var that = this;;
    var param = {
      phone:e.currentTarget.dataset.phone
    };
    wx.showLoading({})
    util.commonRequest('addcarrier', 'post', param, function (res) {
      wx.hideLoading()
      if (res.data.code == 200) {
        wx.showToast({
          title: '添加成功',
          icon: 'success',
          duration: 1500
        })
         
      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        })
      }
    });

    // var index = e.currentTarget.dataset.index;
    // var freight = this.data.freight;
    // freight.splice(index,1);
    // wx.showToast({
    //   title: '加入熟车',
    //   icon: 'success',
    //   duration: 2000,
    // })
    // this.setData({
    //   freight: freight
    // })
  },
  //地址
  start_ads: function (e) {
    var lat = e.currentTarget.dataset.lat;
    var lon = e.currentTarget.dataset.lon;
    var name = e.currentTarget.dataset.name;
    var nickname = e.currentTarget.dataset.nickname;
    var city = e.currentTarget.dataset.city;
    if(lat && lon){
      wx.openLocation({
        latitude: Number(lat),
        longitude:Number(lon),
        scale: 16,
        name: nickname,
        address: city
      })
    }else{
      wx.showToast({
        icon:'loading',
        title: '不能定位',
      })
    }
   
  },
  //拨打电话
  start_phone:function(e){
    var phone = e.currentTarget.dataset.phone;
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  // 选择地址
  chose_city: function (e) {
    var type = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '../address/address?type=' + type,
    })
  },
  typebuy: function () {
    this.setData({
      buytype: !this.data.buytype
    })
  },
  // 车长选择
  chose_baby(e) {
    var id = e.currentTarget.dataset.id;
    var value = e.currentTarget.dataset.value;
    this.setData({
      buyindex: id,
      car_length: value,
      currentPage: 1,
      freight: []
    })
    this.initData();
  },
  chose_buyx: function (e) {
    var id = e.currentTarget.dataset.id;
    var value = e.currentTarget.dataset.value;
    this.setData({
      xingindex: id,
      car_type: value,
      currentPage: 1,
      freight: []
    })
    this.initData();

  },
 
  click: function (e) {
    var id = e.currentTarget.dataset.id;
    this.setData({
      freight: [],
      currentPage: 1,
    })

    if (id == 0) {
      this.setData({
        chose_c: true,
        martop:'170'
      })
    } else {
      this.setData({
        chose_c: false,
        martop: '80'
      })
    }
    this.setData({
      navindex: id
    })
    if (id == 1){
      this.practiceCar();
    }else{
      
      this.initData();
    }
  },
  practiceCar:function(){
    var that = this;
    wx.showLoading({ title: '加载中...',})
    util.commonRequest('practiceCar', 'post', {}, function (res) {
      var data = res.data;
      wx.hideLoading();
      if (res.data.code == 200) {
        var tempArr = data.data.data;
        var oriArr = that.data.freight;
        if (that.data.isXiala == 1) {
          var finalyArr = tempArr
        } else {
          var finalyArr = oriArr.concat(tempArr);
        }
        if (finalyArr.length == 0) {
          that.setData({
            isShowTips: true
          })
        }
        that.setData({
          freight: finalyArr,
          totalPage: res.data.data.last_page
        })

        // console.log(res.data.data)

      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.msg,
        })
      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onPullDownRefresh() {
    this.setData({ isXiala:1})

    var navindex = this.data.navindex;
    if (navindex == 1) {
      //熟车

      setTimeout(() => {
        this.practiceCar();
        this.initCartLength();
        this.initCartType();

        wx.stopPullDownRefresh()
        this.setData({
          city: app.data.address.name || "请选择",
          city1: app.data.address.name || "请选择",
        })
      }, 1000)

    } else {
      //全部

      setTimeout(() => {
        this.initData();
        this.initCartLength();
        this.initCartType();

        wx.stopPullDownRefresh()
        this.setData({
          city: app.data.address.name || "请选择",
          city1: app.data.address.name || "请选择",
        })
      }, 1000)
    }

  },
  onLoad: function (options) {
    var ismy = options.ismy;
    if(ismy == 1){
      //我的车辆
      this.setData({
        ismy:1,
        // showShu: false
      })
      wx.setNavigationBarTitle({
        title: "我的车辆"
      })
    } else{
      this.setData({
        ismy: 0
      })
     
      wx.setNavigationBarTitle({
        title:"车源"
      })
    }
    this.initData();
    this.initCartLength();
    this.initCartType();
    this.initUserData();

    if (options.id == 1) {
      this.setData({
        tabbarIndex: options.id
      })
      getApp().editTabBar1();
    } else {
      getApp().editTabBar();
    }
    let that = this
    if (!app.data.address.name) {
      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          console.log(res);
        },
      })
    }
    that.setData({
      city: app.data.address.name || "请选择",
      city1: app.data.address.name || "请选择",
    })
    wx.getSystemInfo({
      success(res){
        that.setData({
          scrollHeight: res.windowHeight
        });
      }

    }) 
  },
//下拉加载更多
  bindDownLoad(even){
    // var currentpage = this.data.currentPage;
    // var totalpage = this.data.totalPage;
    // if (currentpage < totalpage) {
    //   this.setData({
    //     currentPage: currentpage + 1
    //   })
    //   this.initData();
    // } else {
    //   console.log("已经是全部数据了")
    //   // wx.showToast({
    //   //   'icon':'loading',
    //   //   title: '已经是全部数据了',
    //   // })
    // }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },
  initUserData: function () {
    var that = this;
    util.commonRequest('getuserInfoByToken', 'post', {}, function (res) {
      if (res.data.code == 200) {
        let ismy = that.data.ismy;
        let userType = res.data.data.type;
        
        if (ismy == 1 && userType == 3){
          that.setData({
            showTop:false
          })
        } 

       
      
        
      } else {
        wx.showToast({
          icon: 'loading',
          title: res.data.msg,
        })
      }
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
    
    var currentpage1 = this.data.currentPage1;
    var currentpage2 = this.data.currentPage2;
   
    var navindex = this.data.navindex;
    if (navindex == 1){
      //熟车
      var currentpage = currentpage1;
    } else{
      //全部
      var currentpage = currentpage2;
    }
    var totalpage = this.data.totalPage;
    if (currentpage < totalpage) {
      if (navindex == 1) {
        //熟车
        this.setData({
          currentPage: currentpage + 1,
          currentpage1: currentpage + 1,
        })
        this.practiceCar();
      } else {
        //全部
        this.setData({
          currentPage: currentpage + 1,
          currentpage2: currentpage + 2,
        })
        this.initData();
      }
      
    } else {
      // console.log("已经是全部数据了")
      wx.showToast({
        'icon':'loading',
        title: '已经是全部数据了',
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})